package com.lv.dhart.ui.fragment;

import androidx.recyclerview.widget.GridLayoutManager;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lv.dhart.R;
import com.lv.dhart.adapters.MyAdapter;
import com.lv.dhart.beans.banner.BannerDataListBean;
import com.lv.dhart.beans.market.OnePriceOrders;
import com.lv.dhart.constant.ApiPathConstant;
import com.lv.dhart.custom.fragment.baseframework.BaseActivity;
import com.lv.dhart.custom.fragment.baseframework.BaseFragment;
import com.lv.dhart.custom.fragment.baseframework.interfaces.Layout;
import com.lv.dhart.http.rxeasy.EasyHttp;
import com.lv.dhart.http.rxeasy.callback.SimpleCallBack;
import com.lv.dhart.http.rxeasy.exception.ApiException;
import com.lv.dhart.http.rxeasy.utils.HttpLog;
import com.lv.dhart.utils.GsonUtils;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;

@Layout(R.layout.fragment_issue)
public class IssueFragment extends BaseFragment<BaseActivity> implements OnBannerListener {
    private Banner banner;
    BannerDataListBean bannerDataListBean;
    OnePriceOrders onePriceOrders;
    private XRecyclerView mRecyclerView;
    private MyAdapter myAdapter;
    ArrayList<OnePriceOrders.DataDTO.ListDTO> datas = new ArrayList<>();
    RefreshLayout refreshLayout;

    @Override
    public void initViews() {
        banner = findViewById(R.id.banner);
        mRecyclerView = (XRecyclerView) this.findViewById(R.id.recyclerview);
        GridLayoutManager layoutManager = new GridLayoutManager(requireContext(), 1);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);
        myAdapter = new MyAdapter(datas);
        mRecyclerView.setAdapter(myAdapter);

        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setRefreshHeader(new ClassicsHeader(requireActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(requireActivity()));


    }

    @Override
    public void initDatas() {
        HttpLog.i("this.hashCode() " + this.hashCode());
//        getBannerList();
//        getOnePriceOrders();

    }

    /**
     *
     */
    private void getBannerList() {
        EasyHttp.get(ApiPathConstant.API_PATH_PUBLIC_BANNER_LIST)
                .accessToken(true)
                .execute(
                        new SimpleCallBack<String>() {
                            @Override
                            public void onError(ApiException e) {
                                HttpLog.i(e.getDisplayMessage());
                            }

                            @Override
                            public void onSuccess(String s) {
                                bannerDataListBean = GsonUtils.fromJson(s, BannerDataListBean.class);
                                banner.setDatas(bannerDataListBean.getData());
                                useBanner();
                            }
                        }

                );
    }


    @Override
    public void setEvents() {
        banner.setOnBannerListener(this);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
            }

            @Override
            public void onLoadMore() {
                // load more data here
            }
        });
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                mRecyclerView.refreshComplete();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
            }
        });
    }


    @Override
    /**
     * 进入 Fragment 时调用此方法，isSwitchFragment 标记说明了是否为从别的 Fragment 切换至此 Fragment 的，
     * 若为 false，则有可能是从后台切换至前台触发
     */
    public void onShow(boolean isSwitchFragment) {
        HttpLog.i("IntroductionFragment: onShow");
        super.onShow(isSwitchFragment);
    }

    @Override
    public void onHide() {
        HttpLog.i("IntroductionFragment: onHide");
        super.onHide();
    }

    public void useBanner() {
        //--------------------------简单使用-------------------------------
//        banner.addBannerLifecycleObserver(this)//添加生命周期观察者
//                .setAdapter(new BannerExampleAdapter(DataBean.getTestData()))
//                .setIndicator(new CircleIndicator(requireContext()));

        //—————————————————————————如果你想偷懒，而又只是图片轮播————————————————————————
//        banner.setBannerGalleryMZ(20);
        banner.setBannerGalleryEffect(50, 5);

        banner.setAdapter(new BannerImageAdapter<BannerDataListBean.DataDTO>(bannerDataListBean.getData()) {
                    @Override
                    public void onBindView(BannerImageHolder holder, BannerDataListBean.DataDTO data, int position, int size) {
                        //图片加载自己实现
                        Glide.with(holder.itemView)
                                .load(data.getImageUrl())
                                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                                .into(holder.imageView);
                    }
                })
                .addBannerLifecycleObserver(this)//添加生命周期观察者
                .setIndicator(new CircleIndicator(requireContext()));
        //更多使用方法仔细阅读文档，或者查看demo
    }

    @Override
    public void OnBannerClick(Object o, int i) {
        toast("clikc " + i);
    }
}